import {
  selectAll,
  selectAllDoctor,
  add,
  addNurse,
  update,
  del
} from '@/api/registration'

const getDefaultState = () => {
  return {}
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  // use select all
  selectAll({
    commit
  }, hospitalInfo) {
    return new Promise((resolve, reject) => {
      const {
        obj,
        page,
        pageSize
      } = hospitalInfo
      selectAll({
        obj: obj,
        page: page,
        pageSize: pageSize
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use select all
  selectAllDoctor({
    commit
  }, hospitalInfo) {
    return new Promise((resolve, reject) => {
      const {
        obj,
        page,
        pageSize
      } = hospitalInfo
      selectAllDoctor({
        obj: obj,
        page: page,
        pageSize: pageSize
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use add menu
  add({
    commit
  }, registrationInfo) {
    return new Promise((resolve, reject) => {
      const {
        doctorId,
        patientId,
        isComplete,
        isAppointment,
        isPayment,
        date
      } = registrationInfo
      add({
        doctorId: doctorId,
        patientId: patientId,
        isComplete: isComplete,
        isAppointment: isAppointment,
        isPayment: isPayment,
        date: date
      }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  addNurse({
    commit
  }, registrationInfo) {
    return new Promise((resolve, reject) => {
      const {
        doctorId,
        patientName, 
        patientCard,
        isComplete,
        isAppointment,
        isPayment,
        date
      } = registrationInfo
      addNurse({
        doctorId: doctorId,
        patientName: patientName,
        patientCard: patientCard,
        isComplete: isComplete,
        isAppointment: isAppointment,
        isPayment: isPayment,
        date: date
      }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use update
  update({
    commit
  }, registrationInfo) {
    return new Promise((resolve, reject) => {
      const {
        registrationId,
        doctorId,
        patientId,
        isComplete,
        isAppointment,
        isPayment,
        date,
        diagnose
      } = registrationInfo
      update({
        registrationId: registrationId,
        doctorId: doctorId,
        patientId: patientId,
        isComplete: isComplete,
        isAppointment: isAppointment,
        isPayment: isPayment,
        date: date,
        diagnose: diagnose
      }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use delete
  delete({
    commit
  }, ids) {
    return new Promise((resolve, reject) => {
      del(ids).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
